package assignment.quocthai;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddEditActivity extends Activity {
	TextView tvID;
	EditText etServerName;
	EditText etUserName;
	EditText etPassword;
	int position;
	String action;
	
	public MyAccountDAO myAccount;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_edit_form);
		
		myAccount = new MyAccountDAO(this);
		
		Bundle receiveBundle = getIntent().getExtras();
					
		action = receiveBundle.getString("action");	
		tvID = (TextView) findViewById(R.id.tvID);
		etServerName = (EditText) findViewById(R.id.etServerName);
		etUserName = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
		
		if(action.equals("edit")){	
			position = receiveBundle.getInt("position");								
			
			tvID.setText(AssignmentActivity.myArrayAdapter.getItem(position).id);
			etServerName.setText(AssignmentActivity.myArrayAdapter.getItem(position).serverName);
			etUserName.setText(AssignmentActivity.myArrayAdapter.getItem(position).userName);
			etPassword.setText(AssignmentActivity.myArrayAdapter.getItem(position).password);
		}
		else if(action.equals("new")){
			tvID.setText(action);
		}
				
		Button btSave = (Button) findViewById(R.id.btSave);
		Button btCancel = (Button) findViewById(R.id.btCancel);
		
		btSave.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(tvID.getText().toString().equals("new")){//New
					myAccount.insertAccount(
							etServerName.getText().toString(), 
							etUserName.getText().toString(), 
							etPassword.getText().toString()
							);
					
					String lastID = myAccount.getLastAccountID();
					
					AssignmentActivity.myArrayAdapter.accountListArrayAdapter.add(
							new MyAccountDTO(
								lastID,
								etServerName.getText().toString(), 
								etUserName.getText().toString(), 
								etPassword.getText().toString()
							));
					AssignmentActivity.myArrayAdapter.notifyDataSetChanged();
					finish();
				}
				else{//Edit
					MyAccountDTO updateAccount = new MyAccountDTO(
							AssignmentActivity.myArrayAdapter.getItem(position).id,
							etServerName.getText().toString(), 
							etUserName.getText().toString(), 
							etPassword.getText().toString()); 
					
					myAccount.updateAccount(
							updateAccount.id, 
							updateAccount.serverName, 
							updateAccount.userName, 
							updateAccount.password);
					
					AssignmentActivity.myArrayAdapter.accountListArrayAdapter.set(position, updateAccount);
					AssignmentActivity.myArrayAdapter.notifyDataSetChanged();
					
					finish();
				}
			}
		});
		
		btCancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
}
